package com.atguigu.spzx.manner.config;

import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.myemun.ResultCodeEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import spzx.util.AtguiguAssert;

@RestControllerAdvice(basePackages = "com.atguigu.spzx.manner.controller")
public class GlobalExceptionHandler {

    // 处理业务断言异常（AtguiguAssert抛出的异常）
    @ExceptionHandler(IllegalArgumentException.class)
    public Result handleBusinessException(IllegalArgumentException e) {
        e.printStackTrace();
        return Result.build(  e.getMessage(),ResultCodeEnum.BUSINESS_ERROR);
    }



    // 处理所有未捕获异常
    @ExceptionHandler(Exception.class)
    public Result handleSystemException(Exception e) {
        e.printStackTrace(); // 生产环境应替换为日志记录
        return Result.build(null, ResultCodeEnum.SYSTEM_ERROR);
    }
}
